﻿using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using OperationPlugins.Applications;
using OperationPlugins.Json;

namespace OperationPlugins.WebAppClient
{
    public static class WebApp
    {

        public static void Validate(string address, 
                                    object content, 
                                    string payloadContentAlias, 
                                    string username, 
                                    string password, 
                                    string appId, 
                                    string operationId, 
                                    string branchId)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            if (payloadContentAlias == null)
            {
                throw new ArgumentNullException("payloadContentAlias");
            }

            if (appId == null)
            {
                throw new ArgumentNullException("appId");
            }

            string json = JsonUtil.Serialize(content);

            var data = new NameValueCollection { { "content", json } };

            using (WebClient client = new WebClient())
            {
                client.Encoding = Encoding.UTF8;

                HttpRequestHeaders headers = HttpRequestHeaders.Bind(client.Headers);
                headers.PayloadContentType = "application/json";
                headers.Username = username;
                headers.Password = password;
                headers.AppId = appId;
                headers.OperationId = operationId;
                headers.BranchId = branchId;
                headers.PayloadContentAlias = payloadContentAlias;
                
                byte[] responsebytes = client.UploadValues(address, "POST", data);
                string responsebody = Encoding.UTF8.GetString(responsebytes);
            }
        }


    }
}
